package cn.chint.manage.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.chint.manage.dao.UserDao;
import cn.chint.manage.service.UserService;
import cn.chint.util.BeanUtil;
import cn.chint.util.HTTPClientUtils;
import cn.chint.util.QRCodeUtil;
import cn.chint.util.RequestIp;
import cn.chint.util.UtilDate;

import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
import com.github.binarywang.wxpay.service.EntPayService;
import com.github.binarywang.wxpay.service.WxPayService;

@Controller
public class QrcodeController { 
	@Autowired
	UserService userService;
	@Autowired
	UserDao userDao;

	public Logger log = Logger.getLogger(getClass());
	
	@Value("${chint.qrcode.path}")
	private String qrcode_path;
	
	@Value("${chint.qrcode.url}")
	private String qrcode_url;
	
	/** 生成转发二维码
	  * 获取转发人的opendid。
	  * 添加到URL地址参数后面，生成二维码。
	  * 用户扫描后在解析这个转发人的openid，添加项目成功后，就知道是谁转发过来的。
	  **/
	@RequestMapping(value = "qrcode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String  qrcode(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response)  {
		com.alibaba.fastjson.JSONObject  jsonObject  = new com.alibaba.fastjson.JSONObject ();
		try{
			String zf_openid = request.getParameter("zf_openid");
			log.info("转发人openid="+zf_openid);
			
			String parentDir = request.getServletContext().getRealPath("statics/images");
			log.info(parentDir);
			
			String textt = "http://lead.taiwuyou.cn/chint-dl/msgenter.html?zf_openid="+zf_openid;//二维码的内容
			String logo = parentDir =parentDir +"/logo.jpg";//中间logo地址
			log.info("logo地址："+logo);
			
			String file = new Random().nextInt(99999999) + ".jpg";//生成随机文件名
			log.info(file.toString()+" 文件名");

			String path=qrcode_path;//二维码写入地址
			log.info("二维码写入地址："+path);
			
			QRCodeUtil.encode(textt,logo,path,true,file);
			
			String url  = qrcode_url+file;
			log.info("二维码展示地址:"+url);
			
			jsonObject.put("qrcode_url", url);
			jsonObject.put("status", true);
			
			return jsonObject.toJSONString();
		}catch (Throwable e) {
			e.printStackTrace();
			log.info("错误信息"+e.getMessage());
			jsonObject.put("status", false);
			return jsonObject.toJSONString();
		}
	}


}





